#include "athletemodel.h"
#include <QDebug>
#include <QStringList>

AthleteModel::AthleteModel(SystemController *controller, QObject *parent, QSqlDatabase db) : QSqlTableModel(parent, db)
{
    this->systemController = controller;
    this->parentWidget = (QWidget*) parent;
}

AthleteModel::~AthleteModel()
{
    this->database().close();
}


/*!
  \setData
  Override Function to set the Data of the Model
  */
bool AthleteModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    //Removed existing Whitespaces
    QString strValue = value.toString();
    strValue = strValue.simplified();

    //Check if a Date is changed, that it has the right date format
    if (index.column() == this->BIRTH_DATE_COLUMN || index.column() == this->CID_COLUMN)
    {
        QRegExp regEx("^[0-9]{4,4}-[0-9]{2,2}-[0-9]{2,2}$");
        if (!regEx.exactMatch(strValue))
        {
            this->systemController->showErrorDialog(this->parentWidget, tr("Wrong Format"), tr("Please use the following date format: \"yyyy-MM-dd\""));
            return false;
        }
    }
    return QSqlTableModel::setData(index, strValue, role);
}

/*!
  \data
  Override Function to change the returned content of this cell
  */
QVariant AthleteModel::data(const QModelIndex &idx, int role) const
{
    //Modifications on the text
    if (role == Qt::DisplayRole)
    {
        //If column eq birthDate or CId date:
        if (idx.column() == this->BIRTH_DATE_COLUMN || idx.column() == this->CID_COLUMN)
        {
            QString format = QSqlTableModel::data(idx, role).toString();
            QStringList splitted = format.split("-");
            if (splitted.length() < 3)
                return "";
            else
            {
                QString result = splitted[2] + "." + splitted[1] + "." + splitted[0];
                return result;
            }
        }

        //Translate the Gender
        if (idx.column() == this->GENDER_COLUMN)
        {
            if(QSqlTableModel::data(idx, role).toString()=="male")
                return this->systemController->tranlsateGender(true);
            else
                return this->systemController->tranlsateGender(false);

        }
    }

    //Modification on the Background
    if (role == Qt::BackgroundRole)
    {
        //If column eq CId date:
        if (idx.column() == this->CID_COLUMN)
        {
            int result = this->dateOverdue(QSqlTableModel::data(idx).toString());
            if (result == 1)
            {
                QBrush yellowBackground(Qt::yellow);
                return yellowBackground;
            }
            if (result == 2)
            {
                QBrush redBackground(Qt::red);
                return redBackground;
            }
        }
    }

    return QSqlTableModel::data(idx, role);
}

/*!
  \fn selectStatement
  Function which change the selectStatement, to set an Order
  */
QString AthleteModel::selectStatement() const
{
    QString stmt =  QSqlTableModel::selectStatement();
    int fromPos = stmt.lastIndexOf("FROM");

    QString newSelect = "SELECT \"aId\", \"name\", \"firstName\", \"birthDate\", \"gender\", \"phone\", \"mobile\", \"mail\", \"competitiveIdExp\" ";

    stmt = stmt.remove(0, fromPos);
    stmt = stmt.prepend(newSelect);

    return stmt;
}


/*!
  \fn dateOverdue
  Function with checks if the give date is near or higher then the actual date
  \param dateString: dateString in the format yyyy-MM-dd
  \returns 0 if all ok, 1 if difference between given and actual date < 31, 2 if actual date higher then given date
  */
int AthleteModel::dateOverdue(QString dateString) const
{
    QStringList dateList = dateString.split("-");
    if (dateList.size() < 3)
        return 0;
    QDate givenDate(dateList.at(0).toInt(), dateList.at(1).toInt(), dateList.at(2).toInt());
    QDate today = QDate::currentDate();

    //Check actual date higher then given date
    if (givenDate < today)
        return 2;
    else
    {
        givenDate = givenDate.addDays(-31);
        if (givenDate < today)
            return 1;
        else
            return 0;
    }

}



